package com.yuexin.project.store.outbound.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuexin.common.support.Convert;
import com.yuexin.project.store.outbound.domain.Outbound;
import com.yuexin.project.store.outbound.mapper.OutboundMapper;
import com.yuexin.project.store.stock.domain.Stock;
import com.yuexin.project.store.stock.service.IStockService;

/**
 * 出库单 服务层实现
 * 
 * @author yuexin
 * @date 2018-11-30
 */
@Service
public class OutboundServiceImpl implements IOutboundService {
	@Autowired
	private OutboundMapper outboundMapper;
	
	@Autowired
	private IStockService stockService;

	/**
	 * 查询出库单信息
	 * 
	 * @param outboundId 出库单ID
	 * @return 出库单信息
	 */
	@Override
	public Outbound selectOutboundById(Integer outboundId) {
		return outboundMapper.selectOutboundById(outboundId);
	}

	/**
	 * 查询出库单列表
	 * 
	 * @param outbound 出库单信息
	 * @return 出库单集合
	 */
	@Override
	public List<Outbound> selectOutboundList(Outbound outbound) {
		return outboundMapper.selectOutboundList(outbound);
	}

	/**
	 * 新增出库单
	 * 
	 * @param outbound 出库单信息
	 * @return 结果
	 */
	@Override
	public int insertOutbound(Outbound outbound) {
		//减去物品库存
		Integer goodsId = outbound.getGoodsId();
		Stock stock = stockService.selectStockById(goodsId);
		Integer amountOld = stock.getStockAmount();
		Integer amountNew = outbound.getOutboundNumber();
		amountOld = amountOld - amountNew;
		stock.setStockAmount(amountOld);
		stockService.updateStock(stock);
		
		return outboundMapper.insertOutbound(outbound);
	}

	/**
	 * 修改出库单
	 * 
	 * @param outbound 出库单信息
	 * @return 结果
	 */
	@Override
	public int updateOutbound(Outbound outbound) {
		//先在库存增加修改前的数量，再减去修改后的数量
		Integer outboundId = outbound.getOutboundId();
		Outbound oldOutBound = outboundMapper.selectOutboundById(outboundId);
		Integer oldNumber = oldOutBound.getOutboundNumber();
		
	    Integer goodsId = outbound.getGoodsId();
		Stock stock = stockService.selectStockById(goodsId);
		Integer amount = stock.getStockAmount();
		Integer amountNew = outbound.getOutboundNumber();
		amount = amount + oldNumber - amountNew;
		stock.setStockAmount(amount);
		stockService.updateStock(stock);
		
		return outboundMapper.updateOutbound(outbound);
	}

	/**
	 * 删除出库单对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteOutboundByIds(String ids) {
		String[] idArray = Convert.toStrArray(ids);
		for(int i = 0; i<idArray.length; i++) {
			Integer outboundId = Integer.valueOf(idArray[i]);
			Outbound outbound = outboundMapper.selectOutboundById(outboundId);
			Integer number = outbound.getOutboundNumber();
			
			Integer goodsId = outbound.getGoodsId();
			Stock stock = stockService.selectStockById(goodsId);
			Integer amount = stock.getStockAmount();
			amount = amount + number;
			stock.setStockAmount(amount);
			stockService.updateStock(stock);
			outboundMapper.deleteOutboundById(outboundId);
		}
		
		return idArray.length;
	}
	
	@Override
	public int deleteOutboundById(Integer outboundId) {
		Outbound outbound = outboundMapper.selectOutboundById(outboundId);
		Integer number = outbound.getOutboundNumber();
		
		Integer goodsId = outbound.getGoodsId();
		Stock stock = stockService.selectStockById(goodsId);
		Integer amount = stock.getStockAmount();
		amount = amount + number;
		stock.setStockAmount(amount);
		stockService.updateStock(stock);
		return outboundMapper.deleteOutboundById(outboundId);
	}

}
